//
//  UIImage+tool.cpp
//  iOSBaseFramework
//
//  Created by user on 2020/6/15.
//  Copyright © 2020 com.taylor. All rights reserved.
//

#import "UIImage+tool.h"
#import "NSString+tool.h"
#import "NSString+Encryption.h"

@implementation UIImage (tool)

- (UIImage*)scaleImageToSize:(CGSize)size
{
    UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height));
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return reSizeImage;
}

- (UIImage*)imageReSize:(CGSize)reSize
{
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
    [self drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return reSizeImage;
}

- (UIImage*)cutImageWithRect:(CGRect)cutRect
{
    CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, cutRect);
    UIGraphicsBeginImageContext(cutRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, cutRect, subImageRef);
    UIImage *cutedImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
    if (subImageRef)
    {
        CFRelease(subImageRef);
    }
    return cutedImage;
}


+ (UIImage*)smallTheImage:(UIImage*) image
{
    NSData *data = UIImageJPEGRepresentation(image, 1.);
    if (data.length > 1024*1024*2) {
        NSData *smallData = UIImageJPEGRepresentation(image, (1024*1024*2)/data.length*2);
        if (smallData.length > 1024 * 1024 * 2) {
            return [self smallTheImage:[UIImage imageWithData:smallData]];
        } else {
            return [UIImage imageWithData:smallData];
        }
    } else {
        return image;
    }
}

+ (NSData *)smallTheImageBackData:(UIImage *)image
{
    NSData *data = UIImageJPEGRepresentation(image, 1.);
    if (data.length > 1024*1024*1) {
        NSData *smallData = UIImageJPEGRepresentation(image, (1024*1024*1)/data.length*2);
        if (smallData.length > 1024*1024*1) {
            return [self smallTheImageBackData:[UIImage imageWithData:smallData]];
        } else {
            return smallData;
        }
    } else {
        return data;
    }
}

+  (UIImage*)imageFromView:(UIView*)view
{
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
    } else {
        UIGraphicsBeginImageContext(view.bounds.size);
    }
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:currentContext];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //使当前的context出堆栈
    UIGraphicsEndImageContext();
    return image;
}

- (NSString *)saveImageToSandox:(NSString *)filePath{
    NSString *imagePath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents%@", filePath]];
    [UIImagePNGRepresentation(self) writeToFile:imagePath atomically:YES];
    return imagePath;
}

- (NSString *) saveImageToDocument:(NSString *)filePath {
    NSString *url;
    return url;
}


#pragma mark 图片 - base64互相转换
+ (UIImage *)convertToImageFromBase64:(NSString *)base64Str {
    if ([base64Str isEmptyString]) {
        TRMyLog(@"字符串异常:%@", base64Str);
        return [[UIImage alloc] initWithData:[NSData data]];
    }
    NSData *imageData = [[NSData alloc] initWithBase64EncodedString:base64Str options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
    return[UIImage imageWithData:imageData];
}

- (NSString *)convertToBase64 {
     NSData *data = UIImageJPEGRepresentation(self, 1.0f);
     NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//     MyLog(@"encodedImageStr==%@",encodedImageStr);
    return encodedImageStr;
}
@end
